﻿using Microsoft.AspNetCore.Http;
using ServiceCore.Tool.Post;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace ServiceCore.Tool.Wx
{
    /// <summary>
    /// 通用请求
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class api_base
    {
        private static string url = "";

        private static string access_token = "";
        public api_base create_option(string Url, string Access_token)
        {
            url = Url;
            access_token = Access_token;
            return this;
        }
        /// <summary>
        /// 创建连接并请求
        /// </summary>
        /// <returns></returns>
        public K http_client<T, K>(T Params, bool is_get = true)
        {
            string post_result = "";
            if (is_get)
            {
                string ext_params = "";
                if (Params != null)
                {
                    ext_params = $"&{ParsToString(Params)}";
                }
                post_result = Http_Helper.HttpGet(url + $"?access_token={access_token}{ext_params}");
            }
            else
            {
                post_result = Http_Helper.HttpPost(url + $"?access_token={access_token}", Newtonsoft.Json.JsonConvert.SerializeObject(Params));
            }
            return Newtonsoft.Json.JsonConvert.DeserializeObject<K>(post_result);
        }
        /// <summary>
        /// 创建连接并请求
        /// </summary>
        /// <returns></returns>
        public K post_upload<T, K>(IFormFile file, Dictionary<string, string> post_ext_params_in_url = null, Dictionary<string, string> post_ext_body = null)
        {
            string post_result = "";
            string ext_params = "";
            if (post_ext_params_in_url != null)
            {
                ext_params = $"&{ParsToString(post_ext_params_in_url)}";
            }
            post_result = new upload_http().HttpUploadFile(url + $"?access_token={access_token}{ext_params}", file, post_ext_body, Encoding.UTF8);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<K>(post_result);
        }
        /// <summary>
        /// 参数转为字符串
        /// </summary>
        /// <param name="Pars"></param>
        /// <returns></returns>
        private String ParsToString<T>(T Pars)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var k in typeof(T).GetProperties())
            {
                if (sb.Length > 0)
                {
                    sb.Append("&");
                }
                sb.Append(HttpUtility.UrlEncode(k.Name) + "=" + HttpUtility.UrlEncode(Pars.GetType().GetProperty(k.Name).GetValue(Pars, null).ToString()));
            }
            return sb.ToString();
        }
        /// <summary>
        /// 参数转为字符串
        /// </summary>
        /// <param name="Pars"></param>
        /// <returns></returns>
        private String ParsToString(Dictionary<string, string> Pars)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var k in Pars)
            {
                if (sb.Length > 0)
                {
                    sb.Append("&");
                }
                sb.Append(HttpUtility.UrlEncode(k.Key) + "=" + HttpUtility.UrlEncode(k.Value));
            }
            return sb.ToString();
        }

         

    }
}
